//
// StoreNuke.Net -  http://www.storenuke.net
// Copyright (c) 2005
// by Robert J Collins ( info@storenuke.net ) of WillowTree Software, Inc. ( http://www.willowtreesoftware.com )
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
//

using System;
using System.Data.SqlClient;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace StoreNuke.StarterKit.Commerce.StoreControls
{
	/// <summary>
	///		Summary description for __AddressControl.
	/// </summary>
	public class __AddressControl : UserControl
	{
		protected RequiredFieldValidator RequiredFieldValidator3;
		protected RequiredFieldValidator RequiredFieldValidator2;
		protected RequiredFieldValidator RequiredFieldValidator1;
		protected Label lblAddress1;
		protected Label lblAddress2;
		protected Label lblCity;
		protected RequiredFieldValidator RequiredFieldValidator4;
		protected RequiredFieldValidator RequiredFieldValidator7;
		protected TextBox txtRegion;
		protected Label lblRegion;
		protected Label lblCountry;
		protected DropDownList ddlCountry;
		protected TextBox txtCity;
		protected TextBox txtAddress2;
		protected TextBox txtAddress1;
		protected TextBox txtPhone1;
		protected Label lblPhone1;
		protected TextBox txtPostalCode;
		protected Label lblPostalCode;
		protected TextBox txtPhone2;
		protected Label lblPhone2;
		protected DropDownList ddlRegion;
		protected System.Web.UI.WebControls.Label lblAttn;
		protected System.Web.UI.WebControls.TextBox txtAttn;
		protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator5;
		protected CustomersDB cust = new CustomersDB();

		public bool IsEnabled
		{
			set{SetEnabled(value);}
		}
		
		public string Attn
		{
			get{return txtAttn.Text;}
			set{txtAttn.Text = value;}
		}

		public string AttnTitle
		{
			set{lblAttn.Text = value;}
		}

		public bool AttnVisibility
		{
			set
			{
				txtAttn.Visible = value;
				lblAttn.Visible = value;
			}
		}

		public string Address1
		{
			get{return txtAddress1.Text;}
			set{txtAddress1.Text = value;}
		}

		public string Address2
		{
			get{return txtAddress2.Text;}
			set{txtAddress2.Text = value;}
		}

		public string City
		{
			get{return txtCity.Text;}
			set{txtCity.Text = value;}
		}

		public string Region
		{
			//need to add ddls
			get
			{
				if(ddlRegion.Visible)
				return ddlRegion.SelectedValue;
				else
				return txtRegion.Text;
			}
			set
			{
				if(ddlRegion.Visible)
				ddlRegion.SelectedValue = value;
				else
				txtRegion.Text = value;
			}
		}

		public string Country
		{
			get{return ddlCountry.SelectedValue;}
			set{ddlCountry.SelectedValue = value;}
		}

		public string PostalCode
		{
			get{return txtPostalCode.Text;}
			set{txtPostalCode.Text = value;}
		}

		public string Phone1
		{
			get{return txtPhone1.Text;}
			set{txtPhone1.Text = value;}
		}

		public string Phone2
		{
			get{return txtPhone2.Text;}
			set{txtPhone2.Text = value;}
		}

		private void Page_Load(object sender, EventArgs e)
		{
			//Get Coutries
			if(!Page.IsPostBack)
			{
				ddlCountry.DataSource = cust.GetCountryCode();
				ddlCountry.DataBind();
				GetRegions();
			}
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		///		Required method for Designer support - do not modify
		///		the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.ddlCountry.SelectedIndexChanged += new System.EventHandler(this.ddlCountry_SelectedIndexChanged);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		private void ddlCountry_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			GetRegions();
		}

		private void GetRegions()
		{
			SqlDataReader dr = cust.GetRegionCode(ddlCountry.SelectedValue);
			if(dr.HasRows)
			{
				ddlRegion.DataSource = dr;
				ddlRegion.DataBind();
				ddlRegion.Visible = true;
				txtRegion.Visible = false;
				RequiredFieldValidator3.Visible = false;
			}
			else
			{
				ddlRegion.Visible = false;
				txtRegion.Visible = true;
				RequiredFieldValidator3.Visible = true;
			}
		}

		public void ClearAll()
		{
			foreach(Control ct in this.Controls)
			{
				try
				{
					((TextBox)ct).Text = "";
				}
				catch
				{
				}
			}
		}

		private void SetEnabled(bool value)
		{
			foreach(Control ct in this.Controls)
			{
				try
				{
					((Label)ct).Enabled = value;
				}catch
				{
					try
					{
						((TextBox)ct).Enabled = value;
					}
					catch
					{
						try
						{
							((DropDownList)ct).Enabled = value;
						}
						catch
						{
					
						}
					}
				}
			}
		}
	}
}
